<script setup>
import { ref, reactive, onMounted } from 'vue'
import dayjs from 'dayjs'
import { authAdmin, menuSelectList, userUpdate } from '../../../api/index'
import { Clock } from '@element-plus/icons-vue'
import { useRoute } from 'vue-router'

const route = useRoute()
// 表格数据
let TableData = reactive({
  list: [],
  total: 0,
})

let formData = reactive({
  name: '',
  permissions_id: ''
})

// 权限数据
const options = ref([])

// 弹窗表单引用
const formRef = ref(null)

// 弹窗显示隐藏
const dialogFormVisable = ref(false)


onMounted(async() => {
  getData()

  // 获取权限列表
  const {data} = await menuSelectList()
  options.value = data.data
})

// 根据id匹配权限名称
const permissionName = (id) => {
  return options.value.find((item) => item.id === id).name || '超级管理员'
}

// 分页数据
const paginationData = reactive({
  pageNum: 1,
  pageSize: 10,
})

// 发起请求
async function getData() {
  const { data } = await authAdmin(paginationData)
  TableData.list = data.data.list
  TableData.total = data.data.total

  TableData.list.forEach((item) => {
    item.create_time = dayjs(item.create_time).format('YYYY-MM-DD')
  })
}

// 表单验证规则
const rules = reactive({
  name: [{ required: true, message: '请输入用户名称', trigger: 'blur' }],
  permissions_id: [{ required: true, message: '请选择权限', trigger: 'blur' }],
})


// 表单提交
const confirm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      const {name, permissions_id} = formData
      userUpdate({
        name,
        permissions_id,
      }).then(({ data }) => {
        if (data.code === 10000) {
          ElMessage.success('修改成功')
          // 关闭弹窗
          dialogFormVisable.value = false
          // 重新获取数据
          getData()
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

// 切换页数
function handleSizeChange(val) {
  paginationData.pageSize = val
  getData()
}
// 翻页
function handleCurrentChange(val) {
  paginationData.pageNum = val
  getData()
}
// 打开编辑弹窗
function open(row) {
  formData.name = row.name
  formData.mobile = row.mobile
  formData.permissions_id = row.permissions_id
  dialogFormVisable.value = true
}
// 关闭弹窗
function beforeClose() {
  formData.name = ''
  formData.mobile = ''
  formData.permissions_id = ''
  dialogFormVisable.value = false
}
</script>

<template>
  <PanelHead :routeData='route'></PanelHead>
  <!-- 表格 -->
  <el-table :data="TableData.list" style="width: 100%">
    <el-table-column prop="id" label="id"></el-table-column>
    <el-table-column prop="name" label="名称"></el-table-column>
    <el-table-column prop="permissions_id" label="所属组别">
      <template #default="scope">
        {{ permissionName(scope.row.permissions_id) }}
      </template>
    </el-table-column>
    <el-table-column prop="mobile" label="手机号"></el-table-column>
    <el-table-column label="状态">
      <template #default="scope">
        <el-tag class="ml-2" :type="scope.row.active ? 'success' : 'danger'">{{
          scope.row.active ? '正常' : '异常'
        }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="创建时间">
      <template #default="scope">
        <div class="flex-box">
          <el-icon><Clock></Clock></el-icon>
          <span>{{ scope.row.create_time }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 弹窗表单 -->
  <el-dialog
    v-model="dialogFormVisable"
    :before-close="beforeClose"
    title="修改权限"
    width="500"
  >
    <el-form
      ref="formRef"
      label-width="100px"
      label-position="left"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="formData.mobile" disabled />
      </el-form-item>
      <el-form-item label="昵称" prop="name">
        <el-input v-model="formData.name" ></el-input>
      </el-form-item>
      <el-form-item label="菜单权限" prop="permissions_id">
        <el-select
          v-model="formData.permissions_id"
          placeholder="请选择菜单权限"
          style="width:240px"
        >
          <el-option 
            v-for="item in options" 
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 分页器 -->
  <div class="paginatin-info">
    <el-pagination
      :page-size="paginationData.pageSize"
      :background="false"
      size="small"
      layout="total, prev, pager, next"
      :total="TableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<style lang="less" scoped>
.flex-box {
  display: flex;
  justify-self: center;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
.paginatin-info {
  background-color: #fff;
  height: 40px;
  .el-pagination {
    height: 100%;
    box-sizing: border-box;
    padding: 0 10px;
  }
}
</style>
